package com.shallwead.bna.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.IBinder;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.shallwead.bna.BnA;
import com.shallwead.bna.R;
import com.shallwead.bna.object.AdGcmMsg;

public class PushCircleService extends Service {

	public static int ID_NOTIFICATION = 2018;

	private WindowManager windowManager;
	private RelativeLayout chatHead;
	private Button btn_close;

	boolean mHasDoubleClicked = false;
	long lastPressTime;

	public Context mContext;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		mContext = PushCircleService.this;
		windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

		chatHead = new RelativeLayout(this);
		btn_close = new Button(this);

		// chatHead.setImageResource(R.drawable.ic_launcher);
		Display dis = ((WindowManager) getSystemService(WINDOW_SERVICE))
				.getDefaultDisplay();
		DisplayMetrics metrics = new DisplayMetrics();
		dis.getMetrics(metrics);
		float dpi = metrics.densityDpi;
		float icon_px = 70 / (160 / dpi);
		float close_px = 15 / (160 / dpi);

		// ImageDownloader imgdown = new ImageDownloader(mContext,
		// BnA.gcmMsg.getImageUrl());
		// imgdown.download(BnA.gcmMsg.getImageUrl(), chatHead);
		Drawable drawable = new BitmapDrawable(getResources(), BnA.gcmImg);
		chatHead.setBackgroundDrawable(drawable);

		final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
				(int) icon_px, (int) icon_px,
				WindowManager.LayoutParams.TYPE_PHONE,
				WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
				PixelFormat.TRANSLUCENT);

		params.gravity = Gravity.TOP | Gravity.LEFT;
		params.x = 0;
		params.y = 100;

		final RelativeLayout.LayoutParams btn_params = new RelativeLayout.LayoutParams(
				(int) close_px, (int) close_px);
		btn_params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		btn_params.alignWithParent = true;
		btn_close.setBackgroundResource(R.drawable.bang_btn_x);

		chatHead.addView(btn_close, btn_params);
		windowManager.addView(chatHead, params);

		try {
			chatHead.setOnTouchListener(new View.OnTouchListener() {
				private WindowManager.LayoutParams paramsF = params;
				private int initialX;
				private int initialY;
				private float initialTouchX;
				private float initialTouchY;

				@Override
				public boolean onTouch(View v, MotionEvent event) {
					switch (event.getAction()) {
					case MotionEvent.ACTION_DOWN:
						// Get current time in nano seconds.
						long pressTime = System.currentTimeMillis();
						lastPressTime = pressTime;
						initialX = paramsF.x;
						initialY = paramsF.y;
						initialTouchX = event.getRawX();
						initialTouchY = event.getRawY();
						break;
					case MotionEvent.ACTION_UP:
						long unpressTime = System.currentTimeMillis();
						if (unpressTime - lastPressTime <= 300) {
							BnA.goPushAd(mContext,
									AdGcmMsg.parseAdBack(BnA.gcmMsg));
							PushCircleService.this.stopSelf();
						}
						break;
					case MotionEvent.ACTION_MOVE:
						paramsF.x = initialX
								+ (int) (event.getRawX() - initialTouchX);
						paramsF.y = initialY
								+ (int) (event.getRawY() - initialTouchY);
						windowManager.updateViewLayout(chatHead, paramsF);
						break;
					}
					return false;
				}
			});
		} catch (Exception e) {
			// TODO: handle exception
		}

		chatHead.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {

			}
		});

		btn_close.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				PushCircleService.this.stopSelf();
			}
		});

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (chatHead != null)
			windowManager.removeView(chatHead);
	}

}